package com.yd.tool;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupAutoUrlDB {
	public static void main(String[] args) throws Exception {

		SimpleDateFormat df_ks = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String kaishiTime = df_ks.format(new Date());
		System.out.println("开始----------------" + kaishiTime);

		File autotxt = new File("/Users/shangruihua/Downloads/jsoup/auto/auto_DB.txt");
		File autotxt2 = new File("/Users/shangruihua/Downloads/jsoup/auto/auto2_DB.txt");
		
		
		String urlruls = "http://price.pcauto.com.cn/index/js/5_5/treedata-cn-html.js";
		Document doc = Jsoup.parse(new URL(urlruls).openStream(), "gb2312", urlruls);
		Elements times_content = doc.getElementsByClass("closeChild");
		for (int i = 0; i < times_content.size(); i++) {
			Element che=times_content.get(i);
			
			String id="1000"+i;
			String title=che.select("a").attr("title");
			String tid="0";
			String pid="0";
			String ison="0";
			String shouzimu=UtsHanziToPinyin.getFirstLetter(title);
			String quanpin=UtsHanziToPinyin.toHanyuPinyin(title);
			String img=che.select("img").attr("src2");
			img=img.substring(2,img.length());
			String zhidaojia="0";
			
			String url=che.select("a").attr("onclick").replace("switchSmallTypeFrame(\\'", "").replace("\\');return false;", "");
			
			download("http://"+img,id+(img.substring(img.lastIndexOf("."),img.length())),"/Users/shangruihua/Downloads/jsoup/auto/imgsauto/");
			intxt(autotxt,"insert into citys (idstr,title,tid,pid,ison,shouzimu,quanpin,img,zhidaojia) values ('"+id+"','"+ title+ "','"+tid+"','"+pid+"','"+ison+"','"+ shouzimu+ "','"+ quanpin + "','"+ id+(img.substring(img.lastIndexOf("."),img.length())) + "','"+ zhidaojia + "');");
			
			
			
			String urlu = "http://price.pcauto.com.cn/price/nb"+url+"/";
			Element doccc = Jsoup.connect(urlu).post();

			Elements doccc2 = doccc.getElementsByClass("listTb bigPicList clearfix").select(".bigPic");
			
			
			for (int j = 0; j < doccc2.size(); j++) {
				Element e2=doccc2.get(j);
				
				
				String id_2="2000"+i+"0"+j;
				String title_2=e2.select(".tit").text();
				String tid_2="1";
				String pid_2=id;
				String ison_2="0";
				String shouzimu_2=UtsHanziToPinyin.getFirstLetter(title_2);
				String quanpin_2=UtsHanziToPinyin.toHanyuPinyin(title_2);
				String img_2url=e2.select(".thBig").select(".pic").select("a").select("img").attr("#src");
				img_2url=img_2url.substring(2,img_2url.length());
				String img2=img_2url.substring(img_2url.lastIndexOf("/")+1,img_2url.length());
				String zhidaojia_2=e2.select(".pri").select("span").text();
				
				
				System.out.println(id_2+"_"+title_2+"_"+tid_2+"_"+pid_2+"_"+ison_2+"_"+shouzimu_2+"_"+quanpin_2+"_"+img_2url+"_"+zhidaojia_2);
				
				download("http://"+img_2url,id_2+(img_2url.substring(img_2url.lastIndexOf("."),img_2url.length())),"/Users/shangruihua/Downloads/jsoup/auto/imgsauto/auto2");
				intxt(autotxt2,"insert into citys (idstr,title,tid,pid,ison,shouzimu,quanpin,img,zhidaojia) values ('"+id_2+"','"+ title_2+ "','"+tid_2+"','"+pid_2+"','"+ison_2+"','"+ shouzimu_2+ "','"+ quanpin_2 + "','"+ id_2+(img_2url.substring(img_2url.lastIndexOf("."),img_2url.length())) + "','"+ zhidaojia_2 + "');");
				
			}
			
			
			
		
		}

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("----end----");

		SimpleDateFormat df_js = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String jieshuTime = df_js.format(new Date());
		System.out.println("结束--------" + kaishiTime + "--------" + jieshuTime);

	}

	// 写入txt 追加
	public static void intxt(File file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	//链接url下载图片
	public static void download(String urlString, String filename,String savePath) throws Exception {  
        // 构造URL  
        URL url = new URL(urlString);  
        // 打开连接  
        URLConnection con = url.openConnection();  
        //设置请求超时为5s  
        con.setConnectTimeout(5*1000);  
        // 输入流  
        InputStream is = con.getInputStream();  
      
        // 1K的数据缓冲  
        byte[] bs = new byte[1024];  
        // 读取到的数据长度  
        int len;  
        // 输出的文件流  
       File sf=new File(savePath);  
       if(!sf.exists()){  
           sf.mkdirs();  
       }  
       OutputStream os = new FileOutputStream(sf.getPath()+File.separator+filename);  
        // 开始读取  
        while ((len = is.read(bs)) != -1) {  
          os.write(bs, 0, len);  
        }  
        // 完毕，关闭所有链接  
        os.close();  
        is.close();  
    }   
    
    
}
